package com.fang.mmy.items.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName MyBlockExceptionHandler
 * @Author kebukeyi
 * @Date 2022/8/7 22:27
 * @Description 想使用BlockException统一异常处理时，不能添加@SentinelResource
 * @Version 1.0.0
 */
public class MyBlockExceptionHandler implements BlockExceptionHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        System.out.println("资源规则的详细信息：" + e.getRule());
        JSONObject resultObj = new JSONObject();
        if (e instanceof FlowException) {
            resultObj.put("code", 100);
            resultObj.put("msg", "接口限流");
        }
        if (e instanceof DegradeException) {
            resultObj.put("code", 101);
            resultObj.put("msg", "服务降级");
        }
        if (e instanceof ParamFlowException) {
            resultObj.put("code", 102);
            resultObj.put("msg", "热点参数限流");
        }
        if (e instanceof SystemBlockException) {
            resultObj.put("code", 103);
            resultObj.put("msg", "触发系统保护规则");
        }
        if (e instanceof AuthorityException) {
            resultObj.put("code", 104);
            resultObj.put("msg", "授权规则不通过");
        }
        //返回json数据
        response.setStatus(500);
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.getWriter().write(resultObj.toString());
    }
}
